home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / wc.c < prev   
C/C++ Source or Header  |  1997-09-09  |  2KB  |  99 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  * WC.C
  9.  *
  10.  * Count chars, words, and lines in a file or files or, if no files
  11.  * specified, from stdin.
  12.  *
  13.  * WC [file ... ]
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef AMIGA
  21. #include <lib/version.h>
  22. #else
  23. #include <include/lib/version.h>
  24. #endif
  25.  
  26. IDENT("wc",".2");
  27. DCOPYRIGHT;
  28.  
  29. long t_chars, t_words, t_lines;
  30.  
  31. void wc(FILE *);
  32.  
  33. main(ac, av)
  34. int   ac;
  35. char **av;
  36. {
  37.     int   i;
  38.     int   maxlen = 10;
  39.  
  40.     Ident;
  41.     DCopyright;
  42.  
  43. #ifndef unix
  44.     expand_args(ac, av, &ac, &av);
  45. #endif
  46.  
  47.     for (i = 1; i < ac; ++i) {
  48.     int len = strlen(av[i]);
  49.     if (maxlen < len)
  50.         maxlen = len;
  51.     }
  52.  
  53.     for (i = 1; i < ac; ++i) {
  54.     FILE *fi;
  55.  
  56.     if (fi = fopen(av[i], "r")) {
  57.         printf ("%-*s ", maxlen, av[i]);
  58.         fflush(stdout);
  59.         wc(fi);
  60.         fclose(fi);
  61.     } else {
  62.         printf ("error: %s\n", av[i]);
  63.     }
  64.     }
  65.     printf ("\n%*s %8ld %8ld %8ld\n", maxlen, "TOTAL", t_chars, t_words, t_lines);
  66.     return(0);
  67. }
  68.  
  69. void
  70. wc(fi)
  71. FILE *fi;
  72. {
  73.     int l_chars = 0, l_words = 0, l_lines = 0;
  74.     char buf[256];
  75.     char *ptr;
  76.  
  77.     while (fgets(buf, sizeof(buf), fi)) {
  78.     l_chars += strlen(buf);
  79.  
  80.     ++l_lines;
  81.  
  82.     ptr = buf;
  83.     while (*ptr) {
  84.         while (*ptr == ' ')
  85.         ++ptr;
  86.         if (*ptr) {
  87.         while (*ptr && *ptr != ' ')
  88.             ++ptr;
  89.         ++l_words;
  90.         }
  91.     }
  92.     }
  93.     printf ("%8ld %8ld %8ld\n", l_chars, l_words, l_lines);
  94.     t_chars += l_chars;
  95.     t_words += l_words;
  96.     t_lines += l_lines;
  97. }
  98.  
  99.